<script setup lang="ts">
import { useI18n } from '@/hooks/web/useI18n'
import { ElTag, ElButton ,ElSwitch} from 'element-plus'
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { getProcessProductLogApi } from '@/api/product'
import { ProcessProductLog } from '@/api/product/types'
import { TableColumn } from '@/types/table'
import { useValidator } from '@/hooks/web/useValidator'
import { useEmitt } from '@/hooks/web/useEmitt'
import { h, ref, unref, reactive } from 'vue'
import { FormSchema } from '@/types/form'
import { Table } from '@/components/Table'
import { useTable } from '@/views/Product/useTable'

const { t } = useI18n()

const { required } = useValidator()

const schema = reactive<FormSchema[]>([
  {
    field: 'productCode',
    label: '商品编号',
    component: 'Input'
  },
  {
    field: 'productName',
    label: '商品名称',
    component: 'Input'
  },
  {
    field: 'createDate',
    label: '操作时间',
    component: 'DatePicker'
  }
])

const columns = reactive<TableColumn[]>([
  {
    field: 'product_code',
    label: t('productTable.productCode'),
    search: {
      show: true
    }
  },
  {
    field: 'product_name',
    label: t('productTable.productName'),
    search: {
      show: true
    }
  },
  {
    field: 'price',
    label: '原价'
  },
  {
    field: 'nprice',
    label: '现价'
  },
  {
    field: 'state',
    label: '上下架'
  },
  {
    field: 'true_name',
    label: '操作人员'
  },
  {
    field: 'create_date',
    label: '操作时间',
    search: {
      show: true
    }
  }
])

//获取数据
const { register, tableObject, methods } = useTable<ProcessProductLog>({
  getListApi: getProcessProductLogApi,
  response: {
    list: 'list',
    total: 'total'
  },
  props: {
    columns
  }
})

const { getList, setSearchParams } = methods

getList()

useEmitt({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      tableObject.currentPage = 1
    }
    getList()
  }
})
</script>

<template>
  <ContentWrap :title="t('searchDemo.search')">
    <Search :schema="schema" @search="setSearchParams" @reset="setSearchParams" />
  </ContentWrap>
  <ContentWrap :title="t('productTable.processLog')">
    <Table
      :border="true"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :data="tableObject.tableList.slice((tableObject.currentPage - 1) * tableObject.pageSize,tableObject.currentPage * tableObject.pageSize)"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      @register="register"
    >
    <!--  上下架-->
      <template #state="{ row }:{ row: ProductData }">
        <ElSwitch
            v-model="row.state"
            @click="changeState(row)"
            :active-value="1"
            :inactive-value="2"
            active-color="#13ce66"
            inactive-color="#ff4949"
        />
      </template>
    </Table>
  </ContentWrap>
</template>
